﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using APP.HELPER.Utils.Language;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using Dictionary.ERP;
using Common;
using Extra.ERP;
using Security.ERP;

namespace Dictionary
{
    public class xucBranchAdd : xucBaseAdd
    {
        public delegate void SuccessEventHander(object sender, HRM_BRANCH item);

        private readonly IContainer components = null;
        private CalcEdit calMinimumSalary;
        private LabelControl labelControl6;
        private LabelControl lcAddress;
        private LabelControl lcDescription;
        private LabelControl lcMinimumSalary;
        private LabelControl lcPersonName;
        private LabelControl lcPhone;
        private LabelControl lcQuantity;
        private TextEdit txtAddress;
        private MemoEdit txtDescription;
        private CalcEdit txtFactQuantity;
        private TextEdit txtFax;
        private TextEdit txtPersonName;
        private TextEdit txtPhone;
        private CalcEdit txtQuantity;

        public xucBranchAdd()
        {
            InitializeComponent();
            InitMultiLanguages();
            calMinimumSalary.EditValue = 0;
            txtQuantity.Text = "0";
            txtFactQuantity.Text = "0";
        }

        public event SuccessEventHander Success;

        protected override void Add()
        {
            base.Add();
            base.txtID.Text = new HRM_BRANCH().NewID();
            base.txtNAME.Focus();
        }

        public void Clear()
        {
            base.txtID.Text = "";
            base.txtNAME.Text = "";
            txtAddress.Text = "";
            txtPhone.Text = "";
            calMinimumSalary.EditValue = 0;
            txtFax.Text = "";
            txtQuantity.Text = "0";
            txtFactQuantity.Text = "0";
            txtDescription.Text = "";
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        protected override void Init()
        {
        }

        private void InitializeComponent()
        {
            var manager = new ComponentResourceManager(typeof (xucBranchAdd));
            lcDescription = new LabelControl();
            txtAddress = new TextEdit();
            lcAddress = new LabelControl();
            txtQuantity = new CalcEdit();
            lcQuantity = new LabelControl();
            txtPersonName = new TextEdit();
            lcPersonName = new LabelControl();
            txtPhone = new TextEdit();
            lcPhone = new LabelControl();
            txtFax = new TextEdit();
            labelControl6 = new LabelControl();
            txtDescription = new MemoEdit();
            lcMinimumSalary = new LabelControl();
            calMinimumSalary = new CalcEdit();
            txtFactQuantity = new CalcEdit();
            base.gcInfo.BeginInit();
            base.gcInfo.SuspendLayout();
            base.txtNAME.Properties.BeginInit();
            base.txtID.Properties.BeginInit();
            base.imageCollection2.BeginInit();
            ((ISupportInitialize) base.Err).BeginInit();
            txtAddress.Properties.BeginInit();
            txtQuantity.Properties.BeginInit();
            txtPersonName.Properties.BeginInit();
            txtPhone.Properties.BeginInit();
            txtFax.Properties.BeginInit();
            txtDescription.Properties.BeginInit();
            calMinimumSalary.Properties.BeginInit();
            txtFactQuantity.Properties.BeginInit();
            base.SuspendLayout();
            base.gcInfo.Controls.Add(txtFactQuantity);
            base.gcInfo.Controls.Add(calMinimumSalary);
            base.gcInfo.Controls.Add(lcMinimumSalary);
            base.gcInfo.Controls.Add(txtFax);
            base.gcInfo.Controls.Add(labelControl6);
            base.gcInfo.Controls.Add(txtPhone);
            base.gcInfo.Controls.Add(lcPhone);
            base.gcInfo.Controls.Add(txtPersonName);
            base.gcInfo.Controls.Add(lcPersonName);
            base.gcInfo.Controls.Add(lcQuantity);
            base.gcInfo.Controls.Add(txtQuantity);
            base.gcInfo.Controls.Add(txtAddress);
            base.gcInfo.Controls.Add(lcAddress);
            base.gcInfo.Controls.Add(lcDescription);
            base.gcInfo.Controls.Add(txtDescription);
            base.gcInfo.Size = new Size(0x18f, 0xe2);
            base.gcInfo.TabIndex = 0;
            base.gcInfo.Controls.SetChildIndex(txtDescription, 0);
            base.gcInfo.Controls.SetChildIndex(base.lblName, 0);
            base.gcInfo.Controls.SetChildIndex(base.txtNAME, 0);
            base.gcInfo.Controls.SetChildIndex(base.txtID, 0);
            base.gcInfo.Controls.SetChildIndex(base.lblId, 0);
            base.gcInfo.Controls.SetChildIndex(lcDescription, 0);
            base.gcInfo.Controls.SetChildIndex(lcAddress, 0);
            base.gcInfo.Controls.SetChildIndex(txtAddress, 0);
            base.gcInfo.Controls.SetChildIndex(txtQuantity, 0);
            base.gcInfo.Controls.SetChildIndex(lcQuantity, 0);
            base.gcInfo.Controls.SetChildIndex(lcPersonName, 0);
            base.gcInfo.Controls.SetChildIndex(txtPersonName, 0);
            base.gcInfo.Controls.SetChildIndex(lcPhone, 0);
            base.gcInfo.Controls.SetChildIndex(txtPhone, 0);
            base.gcInfo.Controls.SetChildIndex(labelControl6, 0);
            base.gcInfo.Controls.SetChildIndex(txtFax, 0);
            base.gcInfo.Controls.SetChildIndex(lcMinimumSalary, 0);
            base.gcInfo.Controls.SetChildIndex(calMinimumSalary, 0);
            base.gcInfo.Controls.SetChildIndex(txtFactQuantity, 0);
            base.txtNAME.Location = new Point(0x72, 0x34);
            base.txtNAME.Size = new Size(0x111, 20);
            base.txtNAME.TabIndex = 6;
            base.lblName.Location = new Point(4, 0x3b);
            base.lblName.Size = new Size(0x47, 13);
            base.lblName.TabIndex = 5;
            base.lblName.Text = "T\x00ean chi nh\x00e1nh:";
            base.txtID.Location = new Point(0x72, 0x1a);
            base.txtID.Size = new Size(0x62, 20);
            base.lblId.Size = new Size(0x43, 14);
            base.lblId.Text = "M\x00e3 chi nh\x00e1nh:";
            base.imageCollection2.ImageStream =
                (ImageCollectionStreamer) manager.GetObject("imageCollection2.ImageStream");
            base.btnSave.Location = new Point(0x66, 0xeb);
            base.btnSave.TabIndex = 1;
            base.btnCancel.Location = new Point(0x125, 0xeb);
            base.btnCancel.TabIndex = 3;
            base.btnSaveNew.Location = new Point(200, 0xeb);
            base.btnSaveNew.TabIndex = 2;
            lcDescription.Location = new Point(6, 0xbb);
            lcDescription.Name = "lcDescription";
            lcDescription.Size = new Size(0x27, 13);
            lcDescription.TabIndex = 0x11;
            lcDescription.Text = "Ghi ch\x00fa:";
            txtAddress.Location = new Point(0x72, 0x4e);
            txtAddress.Name = "txtAddress";
            txtAddress.Size = new Size(0x111, 20);
            txtAddress.TabIndex = 8;
            lcAddress.Location = new Point(6, 0x51);
            lcAddress.Name = "lcAddress";
            lcAddress.Size = new Size(0x24, 13);
            lcAddress.TabIndex = 7;
            lcAddress.Text = "Địa chỉ:";
            txtQuantity.Location = new Point(0x126, 0x1a);
            txtQuantity.Name = "txtQuantity";
            txtQuantity.Properties.Buttons.AddRange(new[] {new EditorButton(ButtonPredefines.Combo)});
            txtQuantity.Properties.ReadOnly = true;
            txtQuantity.Size = new Size(0x2d, 20);
            txtQuantity.TabIndex = 3;
            lcQuantity.Location = new Point(0xd9, 0x1d);
            lcQuantity.Name = "lcQuantity";
            lcQuantity.Size = new Size(0x49, 13);
            lcQuantity.TabIndex = 2;
            lcQuantity.Text = "Số lượng (Σ/T):";
            txtPersonName.Location = new Point(0x72, 0x9d);
            txtPersonName.Name = "txtPersonName";
            txtPersonName.Size = new Size(0x111, 20);
            txtPersonName.TabIndex = 0x10;
            lcPersonName.Location = new Point(5, 160);
            lcPersonName.Name = "lcPersonName";
            lcPersonName.Size = new Size(0x48, 13);
            lcPersonName.TabIndex = 15;
            lcPersonName.Text = "Người đại diện:";
            txtPhone.Location = new Point(0x72, 0x68);
            txtPhone.Name = "txtPhone";
            txtPhone.Size = new Size(0x76, 20);
            txtPhone.TabIndex = 10;
            lcPhone.Location = new Point(5, 0x6b);
            lcPhone.Name = "lcPhone";
            lcPhone.Size = new Size(0x35, 13);
            lcPhone.TabIndex = 9;
            lcPhone.Text = "Điện thoại:";
            txtFax.Location = new Point(0x10a, 0x68);
            txtFax.Name = "txtFax";
            txtFax.Size = new Size(0x79, 20);
            txtFax.TabIndex = 12;
            labelControl6.Location = new Point(0xee, 0x6b);
            labelControl6.Name = "labelControl6";
            labelControl6.Size = new Size(0x16, 13);
            labelControl6.TabIndex = 11;
            labelControl6.Text = "Fax:";
            txtDescription.Location = new Point(0x72, 0xb8);
            txtDescription.Name = "txtDescription";
            txtDescription.Size = new Size(0x111, 0x23);
            txtDescription.TabIndex = 0x12;
            lcMinimumSalary.Location = new Point(4, 0x85);
            lcMinimumSalary.Name = "lcMinimumSalary";
            lcMinimumSalary.Size = new Size(0x67, 13);
            lcMinimumSalary.TabIndex = 13;
            lcMinimumSalary.Text = "Lương tối thiểu v\x00f9ng:";
            calMinimumSalary.Location = new Point(0x72, 130);
            calMinimumSalary.Name = "calMinimumSalary";
            calMinimumSalary.Properties.Buttons.AddRange(new[] {new EditorButton(ButtonPredefines.Combo)});
            calMinimumSalary.Properties.DisplayFormat.FormatString = "{0:##,##0}";
            calMinimumSalary.Properties.EditFormat.FormatString = "{0:##,##0}";
            calMinimumSalary.Properties.Mask.UseMaskAsDisplayFormat = true;
            calMinimumSalary.Size = new Size(0x111, 20);
            calMinimumSalary.TabIndex = 14;
            txtFactQuantity.Location = new Point(0x156, 0x1a);
            txtFactQuantity.Name = "txtFactQuantity";
            txtFactQuantity.Properties.Buttons.AddRange(new[] {new EditorButton(ButtonPredefines.Combo)});
            txtFactQuantity.Properties.ReadOnly = true;
            txtFactQuantity.Size = new Size(0x2d, 20);
            txtFactQuantity.TabIndex = 4;
            base.AutoScaleDimensions = new SizeF(6f, 13f);
            base.AutoScaleMode = AutoScaleMode.Font;
            base.Name = "xucBranchAdd";
            base.Size = new Size(400, 0x10d);
            base.gcInfo.EndInit();
            base.gcInfo.ResumeLayout(false);
            base.gcInfo.PerformLayout();
            base.txtNAME.Properties.EndInit();
            base.txtID.Properties.EndInit();
            base.imageCollection2.EndInit();
            ((ISupportInitialize) base.Err).EndInit();
            txtAddress.Properties.EndInit();
            txtQuantity.Properties.EndInit();
            txtPersonName.Properties.EndInit();
            txtPhone.Properties.EndInit();
            txtFax.Properties.EndInit();
            txtDescription.Properties.EndInit();
            calMinimumSalary.Properties.EndInit();
            txtFactQuantity.Properties.EndInit();
            base.ResumeLayout(false);
        }

        private void InitMultiLanguages()
        {
            base.lblId.Text = MultiLanguages.GetString("tbl_BranchAdd", "BranchCode", base.lblId.Text);
            base.lblName.Text = MultiLanguages.GetString("tbl_BranchAdd", "BranchName", base.lblName.Text);
            lcQuantity.Text = MultiLanguages.GetString("tbl_BranchAdd", "Quantity", lcQuantity.Text);
            lcAddress.Text = MultiLanguages.GetString("tbl_BranchAdd", "Address", lcAddress.Text);
            lcPhone.Text = MultiLanguages.GetString("tbl_BranchAdd", "Phone", lcPhone.Text);
            lcMinimumSalary.Text = MultiLanguages.GetString("tbl_BranchAdd", "MinimumSalary", lcMinimumSalary.Text);
            lcPersonName.Text = MultiLanguages.GetString("tbl_BranchAdd", "PersonName", lcPersonName.Text);
            lcDescription.Text = MultiLanguages.GetString("tbl_BranchAdd", "Description", lcDescription.Text);
        }

        private void RaiseSuccessEventHander(HRM_BRANCH item)
        {
            if (Success != null)
            {
                Success(this, item);
            }
        }

        public void SetData(HRM_BRANCH item)
        {
            base.txtID.Text = item.BranchCode;
            SYS_LOG.Insert("Danh S\x00e1ch Chi Nh\x00e1nh", "Xem", base.txtID.Text);
            if (base.m_Status == Common.Class.Actions.Update)
            {
                base.txtID.Properties.ReadOnly = true;
            }
            base.txtNAME.Text = item.BranchName;
            txtAddress.Text = item.Address;
            txtPhone.Text = item.Phone;
            txtFax.Text = item.Fax;
            calMinimumSalary.EditValue = item.MinimumSalary;
            txtPersonName.Text = item.PersonName;
            txtQuantity.Text = item.Quantity.ToString();
            txtFactQuantity.Text = item.FactQuantity.ToString();
            txtDescription.Text = item.Description;
        }

        protected override void txtID_EditValueChanged(object sender, EventArgs e)
        {
            var control = (TextEdit) sender;
            if (control.ErrorText != string.Empty)
            {
                base.Err.SetError(control, string.Empty);
            }
            var hrm_branch = new HRM_BRANCH();
            var hrm_department = new HRM_DEPARTMENT();
            var hrm_group = new HRM_GROUP();
            if ((base.m_Status == Common.Class.Actions.Add) &&
                ((hrm_branch.Exist(control.Text) || hrm_department.Exist(control.Text)) || hrm_group.Exist(control.Text)))
            {
                base.Err.SetError(control, "M\x00e3 đ\x00e3 tồn tại.");
                control.Focus();
            }
        }

        protected override void txtID_KeyDown(object sender, KeyEventArgs e)
        {
            var control = (TextEdit) sender;
            if ((e.KeyCode == Keys.Enter) | (e.KeyCode == Keys.Tab))
            {
                var hrm_branch = new HRM_BRANCH();
                var hrm_department = new HRM_DEPARTMENT();
                var hrm_group = new HRM_GROUP();
                if ((base.m_Status == Common.Class.Actions.Add) &&
                    ((hrm_branch.Exist(control.Text) || hrm_department.Exist(control.Text)) ||
                     hrm_group.Exist(control.Text)))
                {
                    base.Err.SetError(control, "M\x00e3 đ\x00e3 tồn tại.");
                    control.Focus();
                }
            }
        }

        protected override string uc_Change()
        {
            return string.Empty;
        }

        protected override string uc_Delete()
        {
            var item = new HRM_BRANCH
            {
                BranchCode = base.txtID.Text
            };
            string str = item.Delete();
            if (str == "OK")
            {
                RaiseSuccessEventHander(item);
            }
            return str;
        }

        protected override string uc_Save()
        {
            if (MyRule.Get(MyLogin.RoleId, "bbiBranch") != "OK")
            {
                return "";
            }
            if (!MyRule.AllowAdd)
            {
                MyRule.Notify();
                return "";
            }
            if (MyLogin.Level > 0)
            {
                XtraMessageBox.Show("Cấp độ quản l\x00fd của bạn kh\x00f4ng được ph\x00e9p th\x00eam chi nh\x00e1nh!",
                    "Cảnh B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return "";
            }
            SYS_LOG.Insert("Danh S\x00e1ch Chi Nh\x00e1nh", "Th\x00eam", base.txtID.Text);
            base.SetWaitDialogCaption("Đang lưu dữ liệu...");
            Cursor.Current = Cursors.WaitCursor;
            var item = new HRM_BRANCH(base.txtID.Text, base.txtNAME.Text, txtAddress.Text, txtPhone.Text, txtFax.Text,
                decimal.Parse(calMinimumSalary.EditValue.ToString()), txtPersonName.Text, int.Parse(txtQuantity.Text),
                int.Parse(txtFactQuantity.Text), txtDescription.Text);
            string text = item.Insert();
            if (text == "OK")
            {
                RaiseSuccessEventHander(item);
            }
            Cursor.Current = Cursors.Default;
            this.DoHide();
            if (text != "OK")
            {
                XtraMessageBox.Show(text, "Cảnh B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            return text;
        }

        protected override string uc_Update()
        {
            if (MyRule.Get(MyLogin.RoleId, "bbiBranch") != "OK")
            {
                return "";
            }
            if (!MyRule.AllowEdit)
            {
                MyRule.Notify();
                return "";
            }
            SYS_LOG.Insert("Danh S\x00e1ch Chi Nh\x00e1nh", "Cập Nhật", base.txtID.Text);
            base.SetWaitDialogCaption("Đang cập nhật dữ liệu...");
            var item = new HRM_BRANCH(base.txtID.Text, base.txtNAME.Text, txtAddress.Text, txtPhone.Text, txtFax.Text,
                decimal.Parse(calMinimumSalary.EditValue.ToString()), txtPersonName.Text, int.Parse(txtQuantity.Text),
                int.Parse(txtFactQuantity.Text), txtDescription.Text);
            string text = item.Update();
            if (text == "OK")
            {
                RaiseSuccessEventHander(item);
            }
            if (text != "OK")
            {
                XtraMessageBox.Show(text, "Cảnh B\x00e1o", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            this.DoHide();
            return text;
        }
    }
}